package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.R;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.UIUtil;

public class JinbiHistoryAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	private Activity activity;
	private int type;

	public JinbiHistoryAdapter(Activity activity, List<JSONObject> itemList, int type) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
		this.type = type;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		if(type!=0)count++;
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public TextView date,title,jinbi,nomore;
		public LinearLayout lay;
		public View line;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int main_tandian_item = R.layout.main_tandian_item;
			convertView = LayoutInflater.from(appContext).inflate(main_tandian_item, null);
			holder.date = (TextView) convertView.findViewById(R.id.main_tandian_item_date);
			holder.title = (TextView) convertView.findViewById(R.id.main_tandian_item_title);
			holder.title.setGravity(type==2?Gravity.LEFT:Gravity.CENTER);
			holder.jinbi = (TextView) convertView.findViewById(R.id.main_tandian_item_size);
			
			holder.lay = (LinearLayout) convertView.findViewById(R.id.main_tandian_item_lay);
			holder.line = convertView.findViewById(R.id.main_tandian_item_line);

			holder.nomore = (TextView) convertView.findViewById(R.id.main_tandian_item_nomore);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		holder.nomore.setVisibility(position==itemList.size()&&pageEnd?View.VISIBLE:View.GONE);
		holder.nomore.setText(itemList.size()==0?"没有内容~":"没有更多了~");
		holder.nomore.setBackgroundColor(activity.getResources().getColor(itemList.size()==0?R.color.color_fa:R.color.color_f3));
		if(position==0&&type!=0){
			String col1 = "";
			String col2 = "";
			String col3 = "";
			switch (type) {
			case 1:
				col1 = "项目";
				col2 = "金币";
				col3 = "时间";
				break;
			case 2:
				col1 = "描述";
				col2 = "金币";
				col3 = "时间";
				break;
			case 3:
				col1 = "描述";
				col2 = "金币";
				col3 = "时间";
				break;
			}
			holder.title.setTextColor(activity.getResources().getColor(R.color.color_64));
			holder.jinbi.setTextColor(activity.getResources().getColor(R.color.color_64));
			holder.date.setTextColor(activity.getResources().getColor(R.color.color_64));
			holder.title.setText(col1);
			holder.jinbi.setText(col2);
			holder.date.setText(col3);
			System.out.println("col3: " + col3);
			holder.lay.setBackgroundColor(activity.getResources().getColor(R.color.color_f3));
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
			lp.setMargins(0, 0, 0, 0);
			holder.line.setLayoutParams(lp);
		}else{
			holder.title.setTextColor(activity.getResources().getColor(R.color.color_1e));
			holder.jinbi.setTextColor(activity.getResources().getColor(R.color.color_1e));
			holder.date.setTextColor(activity.getResources().getColor(R.color.color_1e));
			holder.lay.setBackgroundColor(activity.getResources().getColor(R.color.white));
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
			lp.setMargins(UIUtil.dip2px(activity, 15), 0, UIUtil.dip2px(activity, 15), 0);
			holder.line.setLayoutParams(lp);
			try {
				JSONObject json = itemList.get(type==0?position:(position-1));
				String col1 = "";
				String col2 = "";
				String col3 = "";
				switch (type) {
				case 0:
					col1 = (BaseTools.getFrom("user", json)+" 支付宝提现");
					break;
				case 1:
					col1 = BaseTools.getFrom("summary", json);
					col2 = "+"+BaseTools.getFrom("fees", json, "0")+"金币";
					col3 = BaseTools.getFrom("create_at", json);
					break;
				case 2:
					col1 = BaseTools.getFrom("activity_name", json)+"<br>&nbsp;<br><font color='#646464'>【"+BaseTools.getFrom("status_str", json)+"】"+BaseTools.getFrom("other_info", json)+"</font>";
					col2 = BaseTools.getFrom("fees", json, "0")+"金币";
					col3 = BaseTools.getFrom("create_at", json);//update_at
					break;	
				case 3:
					col3 = BaseTools.getFrom("create_at", json);
					col2 = "-"+BaseTools.getFrom("fees", json, "0")+"金币";
					if(BaseTools.getFrom("status", json).equals("1")){
						col1 = "【已成功】";
					}else if(BaseTools.getFrom("status", json).equals("0")){
						col1 = "【提现中】";
					}else{
						col1 = "【被拒绝】<br>&nbsp;<br><font color='#646464'>"+BaseTools.getFrom("remarks", json)+"</font>";
					}
					break;
				}
				
				System.out.println("col1 : " + col1 + "----" + "col2 : " + col2 + "----" + "col3 : " + col3 );
				holder.title.setText((type==2||type==3)?Html.fromHtml(col1):col1);
				holder.jinbi.setText(col2);
				col3 = col3.length()>=10?col3.substring(0, 10):col3;
				holder.date.setText(col3.substring(0, 10));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return convertView;
	}
}
